package _thread_lock._worktest;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

/**
 * @Author: huangya
 * @Date: 2020/12/11
 * @Description:
 */
public class Channel {

    private BlockingQueue<Product> productQueue = new ArrayBlockingQueue(15);

    Worker[] workers;

    public Channel(int workerCount) {
        workers = new Worker[workerCount];
        for(int i=0; i<workerCount; i++){
            workers[i] = new Worker("thread-workerCount" + i);
            workers[i].start();
        }
    }

    public void offer(Product product){
        productQueue.offer(product);
    }

    private class Worker extends Thread {

        public Worker(String name) {
            super(name);
        }

        @Override
        public void run() {
            Product product = null;
            while (true){
                try {
                    product = productQueue.take();
                    product.create();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        }
    }

}
